package com.example.im.utils;

import com.example.common.interfaces.CommonCallback;
import com.example.im.bean.MessageBean;
import com.example.im.bean.UserInfoBean;
import com.example.im.interfaces.ImClient;
import com.example.im.interfaces.SendMsgResultCallback;
import com.tencent.imsdk.v2.V2TIMConversationResult;
import com.tencent.imsdk.v2.V2TIMMessage;

import java.util.List;

public class MessageUtils {

    private static MessageUtils sInstance;
    private ImClient mImClient;

    private MessageUtils() {
        mImClient = new TencentImUtils();
    }

    public static MessageUtils getInstance() {
        if (sInstance == null) {
            synchronized (MessageUtils.class) {
                if (sInstance == null) {
                    sInstance = new MessageUtils();
                }
            }
        }

        return sInstance;
    }

    /**
     * 初始化 IM
     */
    public void initIm() {
        mImClient.init();
    }

    /**
     * 登录 IM
     *
     * @param uid
     */
    public void login(String uid,CommonCallback<Boolean> callback) {
        mImClient.login(uid,callback);
    }

    /**
     * 获取对话列表
     * @param nextSeq
     * @param count
     * @param callback
     */
    public void getConversationList(int nextSeq, int count, CommonCallback<V2TIMConversationResult> callback) {
        mImClient.getConversationList(nextSeq, count, callback);
    }

    /**
     * 获取历史消息列表
     *
     * @param uid
     * @param count
     * @param lastMsg
     * @param callback
     */
    public void getMessageList(String uid, int count, V2TIMMessage lastMsg, CommonCallback callback) {
        mImClient.getHistoryMessageList(uid, count, lastMsg, callback);
    }

    /**
     * 处理消息为已读
     * @param uid 好友 Id
     */
    public void markMessageAsRead(String uid) {
        mImClient.markConversationAsRead(uid, false, new CommonCallback<Boolean>() {
            @Override
            public void callback(Boolean data) {
                //消息已读
            }
        });
    }

    /**
     * 发送消息
     * @param uid 对方 Id
     * @param text 消息文本
     */
    public void sendTextMessage(String uid, String text, SendMsgResultCallback callback) {
        mImClient.sendTextMessage(uid, text, callback);
    }

    /**
     * 获取好友资料
     * @param uIds uid,多个id逗号隔开
     */
    public void getUserInfo(String uIds, CommonCallback<List<UserInfoBean>> callback) {
        mImClient.getUserInfo(uIds, callback);
    }

    /**
     * 消息监听
     * @param callback
     */
    public void setOnMessageListener(CommonCallback<MessageBean> callback) {
        mImClient.setOnMessageListener(callback);
    }

    /**
     * 离线推送
     * @param business
     * @param uid
     * @param callback
     */
    public void setOfflinePush(long business, String uid, CommonCallback<Boolean> callback) {
        mImClient.setOfflinePush(business, uid, callback);
    }


}
